package com.tangchao.client.sdh.view
{
	import com.tangchao.client.sdh.definition.GlobalDefine;
	import com.tangchao.client.sdh.sdhinterface.Out;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class DiFenSet extends Sprite
	{
		private var _selectModel:int; //0-普通 1-心跳
		
		private var _mcPT:MovieClip;
		private var _mcXT:MovieClip;
		
		private var _btnStart:SimpleButton;

		private var _btnXTAdd:SimpleButton;
		private var _btnXTSub:SimpleButton;
		private var _btnPTAdd:SimpleButton;
		private var _btnPTSub:SimpleButton;
		private var _tfPTDifen:TextField;
		private var _tfXTDifen:TextField;
		private var _btnAccept:SimpleButton;
		
		private var _tfMsg:TextField;
		
		public function DiFenSet()
		{
			super();
			
			_mcPT = getChildByName("mcPT") as MovieClip;
			_mcXT = getChildByName("mcXT") as MovieClip;
			
			_mcPT.stop();
			_mcXT.stop();
			_mcPT.addEventListener(MouseEvent.CLICK, onSelectPT);
			_mcXT.addEventListener(MouseEvent.CLICK, onSelectXT);
			
			_btnStart = getChildByName("btnStart") as SimpleButton;
			_btnStart.addEventListener(MouseEvent.CLICK, onStart);
			
			_btnXTAdd = getChildByName("btnXTAdd") as SimpleButton;
			_btnXTSub = getChildByName("btnXTSub") as SimpleButton;
			_btnPTAdd = getChildByName("btnPTAdd") as SimpleButton;
			_btnPTSub = getChildByName("btnPTSub") as SimpleButton;
			_btnAccept= getChildByName("btnAccept") as SimpleButton;
			_btnAccept.visible = false;
			
			_btnXTAdd.addEventListener(MouseEvent.CLICK, onXTAdd);
			_btnXTSub.addEventListener(MouseEvent.CLICK, onXTSub);
			_btnPTAdd.addEventListener(MouseEvent.CLICK, onPTAdd);
			_btnPTSub.addEventListener(MouseEvent.CLICK, onPTSub);
			_btnAccept.addEventListener(MouseEvent.CLICK, onAccept);
			
			_tfPTDifen = getChildByName("tfPTMark") as TextField;
			_tfXTDifen = getChildByName("tfXTDifen") as TextField;
			_tfMsg     = getChildByName("tfDiFenMsg") as TextField;
			_tfMsg.text = GlobalDefine.DIFENMSG;
			setSelectModel(_selectModel);
		}
		
		private function onSelectPT(e:MouseEvent):void
		{
			setSelectModel(0);
		}
		
		private function onSelectXT(e:MouseEvent):void
		{
			setSelectModel(1);
		}
		
		private function setSelectModel(model:int):void
		{
			_mcPT.gotoAndStop(1);
			_mcXT.gotoAndStop(1);
			
			_selectModel = model;
			if(model == 0)
			{
				_mcPT.gotoAndStop(2);
			}
			
			if(model == 1)
			{
				_mcXT.gotoAndStop(2);
			}
		}
		
		private function get ptDifen():int
		{
			return parseInt(_tfPTDifen.text);
		}
		
		private function get xtDifen():int
		{
			return parseInt(_tfXTDifen.text);
		}
		
		private function onPTAdd(e:MouseEvent):void
		{
			if(ptDifen < GlobalDefine.PTDIFENMAX)
			{
				_tfPTDifen.text = (ptDifen+50).toString();
				_btnStart.visible = true;
			}
		}
		
		private function onPTSub(e:MouseEvent):void
		{
			if(ptDifen > 50 )
			{
				_tfPTDifen.text = (ptDifen-50).toString();
				_btnStart.visible = true;
			}
		}
		
		private function onXTAdd(e:MouseEvent):void
		{
			if(xtDifen < GlobalDefine.XTDIFENMAX)
			{
				_tfXTDifen.text = (xtDifen+50).toString();
				_btnStart.visible = true;
			}
		}
		
		private function onXTSub(e:MouseEvent):void
		{
			if(xtDifen >= 50)
			{
				_tfXTDifen.text = (xtDifen-50).toString();
				_btnStart.visible = true;
			}
		}
		
		private function onAccept(e:MouseEvent):void
		{
			if(_selectModel)
			{
				Out.getInstance().onSetScoreMode(true, _selectModel, xtDifen);
			}
			else
			{
				Out.getInstance().onSetScoreMode(true, _selectModel, ptDifen);
			}
		}
				
		public function userSetScoreModel(scoreName:String, mode:int, score:int, modeName:String, isAllAplly:Boolean, isSelf:Boolean):void
		{
			if(isSelf)
			{
				_btnStart.visible  = false;
				_btnAccept.visible = false;
			}
			else
			{
				_btnStart.visible = false;
				_btnAccept.visible = true;
			}
			
			if(mode == 0)
			{
				_tfPTDifen.text = score.toString();
			}
			else
			{
				_tfXTDifen.text = score.toString();
			}
			setSelectModel(mode);
			
		}
		
		private function onStart(e:MouseEvent):void
		{
			if(_selectModel)
			{
				Out.getInstance().onSetScoreMode(false, _selectModel, xtDifen);
			}
			else
			{
				Out.getInstance().onSetScoreMode(false, _selectModel, ptDifen);
			}
		}
	}
}